package com.atguigu.crud.controller;

import com.atguigu.crud.bean.Command;
import com.atguigu.crud.bean.Msg;
import com.atguigu.crud.bean.Recordsinfo;
import com.atguigu.crud.service.CommandService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 口令
 * @author lfy
 *
 */
@CrossOrigin
@Controller
@RequestMapping("/com")
public class CommandController {
	@Autowired
	private CommandService commandService;
	/*条件查询消息记录
	*/
	@RequestMapping("/getAll")
	@ResponseBody
	public Msg getAll(Command command){
		return Msg.success().add("list", commandService.getAllByPars(command));
	}
	/**
	 * 2、根据id查询
	 * @PathVariable("id") 获取动态参数
	 * @param id
	 * @return
	 * @PathVariable 映射 URL 绑定的占位符
	 */
	@RequestMapping(value="/command/{id}",method= RequestMethod.GET)
	@ResponseBody
	public Msg getEmp(@PathVariable("id")String id){
		return Msg.success().add("command", commandService.getRecord(id));
	}
	/**
	 * 3、保存
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@ResponseBody
	public Msg save(@RequestBody Command command){
		try {
			commandService.saveRec(command);
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail().add("error",e.getMessage());
		}
		return Msg.success();
	}

	/**
	 * 4、通过id删除
	 */
	@RequestMapping(value="/del",method=RequestMethod.GET)
	@ResponseBody
	public Msg del(@RequestParam("id")String id){
		try {
			commandService.delByid(id);
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail().add("error",e.getMessage());
		}
		return Msg.success();
	}

	/**
	 * 5、更新
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Msg update(@RequestBody Command command){
		try {
			commandService.updateRec(command);
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail().add("error",e.getMessage());
		}
		return Msg.success();
	}


}
